home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 February: Tool Chest / Dev.CD Feb 97 TC.toast / Sample Code / Interapplication Communication / MenuScripter 4.0 / Sources / MSMain.c < prev    next >
Encoding:
Text File  |  1996-07-09  |  20.5 KB  |  934 lines  |  [TEXT/CWIE]

  1. // MSMain.c
  2. //
  3. // Original version by Jon Lansdell and Nigel Humphreys.
  4. // 4.0 and 3.1 updates by Greg Sutton.
  5. // Human Interface changes and GX Printing by Don Swatman
  6. // ©Apple Computer Inc 1996, all rights reserved.
  7.  
  8. /*    
  9.     Changes for 3.1
  10.  
  11.     12-Oct-95 : CW : Added call to InitDragHandlers in DoMenuScripter
  12.     12-Oct-95 : CW : Changed MaintainCursor to make cursor arrow if over selected text
  13.     1-Nov-95  : DS : Made Changes for GX Printing.
  14.  
  15.     Changes for 4.0
  16.  
  17.     26-Feb-96 : DS : Improved Edit Menu Handling
  18.     28-Feb-96 : GS : Checked that window did actually move before sending a
  19.                         move event in IssueMoveWindow()
  20. */
  21.  
  22. #include <Memory.h>
  23. #include <Quickdraw.h>
  24. #include <Menus.h>
  25. #include <Windows.h>
  26. #include <Dialogs.h>
  27. #include <Traps.h>
  28. #include <Packages.h>
  29. #include <PPCToolBox.h>
  30. #include <Editions.h>
  31. #include <Printing.h>
  32. #include <ToolUtils.h>
  33. #include <Desk.h>
  34. #include <Scrap.h>
  35. #include <OSEvents.h>
  36. #include <AppleEvents.h>
  37. #include <AEObjects.h>
  38. #include <Errors.h>
  39. #include <limits.h>
  40.  
  41. #include "MSMain.h"
  42.  
  43. #include "MSScript.h"
  44. #include "MSASSubroutines.h"
  45. #include "MSGlobals.h"
  46. #include "MSUtils.h"
  47. #include "MSAERecording.h"
  48. #include "MSAppleEvents.h"
  49. #include "MSWindow.h"
  50. #include "MSFile.h"
  51. #include "MSDrag.h"
  52. #include "MSGXPrinting.h"
  53. #include "MSResultWind.h"
  54. #include "MSAEWindowUtils.h"
  55.  
  56.  
  57.     // Prototypes
  58. void    MaintainCursor( void );
  59. void    SetUpCursors( void );
  60. void    SetUpMenus( void );
  61. void    InitApplicationRec( void );
  62.  
  63. void    DoFile( short theItem );
  64. void    DoMouseDown( const EventRecord *myEvent );
  65. long    GetSleep( void );
  66. void    HandleOneEvent( EventRecord *myEvent );
  67. void    MainEvent( void );
  68.  
  69. void    DoMenuScripter( void );
  70.  
  71.     // Constants
  72. const    short    NeedSys7Alert    = 302;
  73. const    short    AboutAlert        = 258;
  74.  
  75.  
  76. #pragma segment Main
  77.  
  78. void    MaintainCursor( void )
  79. {
  80.     Point     pt;
  81.     WindowPtr wPtr;
  82.     GrafPtr   savePort;
  83.     DPtr      theDoc;
  84.  
  85.     wPtr = FrontWindow();
  86.     if (Ours(wPtr))
  87.     {
  88.         theDoc = DPtrFromWindowPtr(wPtr);
  89.         GetPort(&savePort);
  90.         SetPort(wPtr);
  91.         GetMouse(&pt);
  92.         if (theDoc->theText)
  93.             if ( PtInRect ( pt, &(**(theDoc->theText)).viewRect ) &&
  94.                                             ! PointInWindowSelection ( pt, wPtr ) )
  95.                 SetCursor(&editCursor);
  96.             else
  97.                 SetCursor(&qd.arrow);
  98.         else
  99.             SetCursor(&qd.arrow);
  100.  
  101.         if (theDoc->theText)
  102.             TEIdle(theDoc->theText);
  103.  
  104.         SetPort(savePort);
  105.     }
  106. }
  107.  
  108. #pragma segment Main
  109.  
  110. void MaintainEditItems( TEHandle           theTE,
  111.                         short              numTypes,
  112.                         ConstSFTypeListPtr typeList )
  113. {
  114.     Boolean enableItems;
  115.     short   itemCount;
  116.     long    offset;
  117.     
  118.         // If something is selected then enable cut, copy and clear
  119.     enableItems = false;
  120.     if (theTE)
  121.         if ( (*theTE)->selStart < (*theTE)->selEnd)
  122.             enableItems = true;
  123.         
  124.     SetMenuItemState ( enableItems, myMenus[editM], cutCommand);
  125.     SetMenuItemState ( enableItems, myMenus[editM], copyCommand);
  126.     SetMenuItemState ( enableItems, myMenus[editM], clearCommand );
  127.  
  128.         // if the appropriate type is in scrap, then enable the pasteCommand
  129.     enableItems = false;
  130.     itemCount = 0;
  131.     while ((itemCount < numTypes) && !enableItems)
  132.     {
  133.         enableItems = GetScrap ( nil, typeList[itemCount], &offset);
  134.         ++itemCount;
  135.     }
  136.     
  137.     SetMenuItemState ( enableItems, myMenus[editM], pasteCommand );
  138. }
  139.  
  140.  
  141. void    MaintainMenus( Boolean *pRedrawMenuBar )
  142. {
  143.     DPtr       theDoc;
  144.     WindowPtr  firstWindow;
  145.     SFTypeList myTypes = {'TEXT'};
  146.  
  147.     firstWindow = FrontWindow( );
  148.     if ( ! Ours( firstWindow ) )
  149.     {
  150.         SetMenuItemState( true, myMenus[fileM], fmNew );
  151.         SetMenuItemState( true, myMenus[fileM], fmOpen );
  152.         SetMenuItemState( false, myMenus[fileM], fmClose );
  153.         SetMenuItemState( false, myMenus[fileM], fmSave );
  154.         SetMenuItemState( false, myMenus[fileM], fmSaveAs );
  155.         SetMenuItemState( false, myMenus[fileM], fmRevert );
  156.         SetMenuItemState( false, myMenus[fileM], fmPageSetUp );
  157.         SetMenuItemState( false, myMenus[scriptM], cCompile );
  158.         SetMenuItemState( false, myMenus[scriptM], cExecute );
  159.         
  160.         SetMenuItemState( false, myMenus[scriptM], cResultWindow);
  161.  
  162.         if ( gGXIsPresent )
  163.         {
  164.             SetMenuItemState( false, myMenus[fileM], fmPrint );
  165.             SetMenuItemState( false, myMenus[fileM], fmPrintOne );
  166.         }
  167.         else
  168.             SetMenuItemState( false, myMenus[fileM], fmNoGXPrint );
  169.                         
  170.         if (firstWindow)
  171.         {
  172.             SetMenuItemState( true, myMenus[editM], undoCommand );
  173.             SetMenuItemState( true, myMenus[editM], cutCommand );
  174.             SetMenuItemState( true, myMenus[editM], copyCommand );
  175.             SetMenuItemState( true, myMenus[editM], pasteCommand );
  176.             SetMenuItemState( true, myMenus[editM], clearCommand );
  177.         }
  178.     }
  179.     else
  180.     {
  181.         theDoc = DPtrFromWindowPtr( firstWindow );
  182.  
  183.         if ( theDoc->windowType == kOrdinaryWind )
  184.         {
  185.             *pRedrawMenuBar |= SetMenuItemState( true, myMenus[fontM], kMenuTitle );
  186.             *pRedrawMenuBar |= SetMenuItemState( true, myMenus[sizeM], kMenuTitle );
  187.             *pRedrawMenuBar |= SetMenuItemState( true, myMenus[styleM], kMenuTitle );
  188.  
  189.             SetMenuItemState ( true, myMenus[fileM], fmClose );
  190.             SetMenuItemState ( true, myMenus[fileM], fmSaveAs );
  191.     
  192.             SetMenuItemState( true, myMenus[fileM], fmPageSetUp );
  193.     
  194.             if (gGXIsPresent)
  195.             {
  196.                 SetMenuItemState( true, myMenus[fileM], fmPrint );
  197.                 SetMenuItemState( true, myMenus[fileM], fmPrintOne );
  198.             }
  199.             else
  200.                 SetMenuItemState( true, myMenus[fileM], fmNoGXPrint );                
  201.     
  202.             if (theDoc->dirty)
  203.                 SetMenuItemState( true, myMenus[fileM], fmRevert );
  204.             else
  205.                 SetMenuItemState( false, myMenus[fileM], fmRevert );
  206.                 
  207.             if ((theDoc->dirty) && (theDoc->everSaved))
  208.                 SetMenuItemState( true, myMenus[fileM], fmSave );
  209.             else
  210.                 SetMenuItemState( false, myMenus[fileM], fmSave );
  211.     
  212.             SetMenuItemState( false, myMenus[editM], undoCommand );
  213.             
  214.             SetMenuItemState( true, myMenus[editM], selectAllCommand );
  215.     
  216.             SetMenuItemState( true, myMenus[scriptM], cCompile );
  217.             SetMenuItemState( true, myMenus[scriptM], cExecute );
  218.             
  219.             EnableAEScriptItems( true );
  220.             
  221.             MaintainEditItems( theDoc->theText, 1, myTypes );
  222.         }
  223.         else
  224.         {
  225.             *pRedrawMenuBar |= SetMenuItemState( false, myMenus[fontM], kMenuTitle );
  226.             *pRedrawMenuBar |= SetMenuItemState( false, myMenus[sizeM], kMenuTitle );
  227.             *pRedrawMenuBar |= SetMenuItemState( false, myMenus[styleM], kMenuTitle );
  228.  
  229.             SetMenuItemState( true, myMenus[fileM], fmClose );
  230.             
  231.             SetMenuItemState( false, myMenus[fileM], fmSaveAs );
  232.             SetMenuItemState( false, myMenus[fileM], fmSave );
  233.     
  234.             SetMenuItemState( true, myMenus[fileM], fmPageSetUp );
  235.     
  236.             if ( gGXIsPresent )
  237.             {
  238.                 SetMenuItemState( true, myMenus[fileM], fmPrint );
  239.                 SetMenuItemState( true, myMenus[fileM], fmPrintOne );
  240.             }
  241.             else
  242.                 SetMenuItemState( true, myMenus[fileM], fmNoGXPrint );                
  243.     
  244.             SetMenuItemState( false, myMenus[fileM], fmRevert );
  245.     
  246.             SetMenuItemState( false, myMenus[editM], pasteCommand );
  247.             SetMenuItemState( false, myMenus[editM], undoCommand );
  248.             SetMenuItemState( false, myMenus[editM], cutCommand );
  249.             SetMenuItemState( false, myMenus[editM], copyCommand );
  250.             SetMenuItemState( false, myMenus[editM], clearCommand );
  251.             SetMenuItemState( false, myMenus[editM], selectAllCommand );
  252.  
  253.             SetMenuItemState( false, myMenus[scriptM], cCompile );
  254.             SetMenuItemState( false, myMenus[scriptM], cExecute );
  255.             
  256.             EnableAEScriptItems( false );
  257.         }
  258.  
  259.         SetMenuItemState( true, myMenus[scriptM], cResultWindow );
  260.         
  261.         if ( IsThereAResultWind( ) )
  262.             SetItemMark( myMenus[scriptM], cResultWindow, checkMark );
  263.         else
  264.             SetItemMark( myMenus[scriptM], cResultWindow, noMark );
  265.     }
  266.  
  267.     if ( *pRedrawMenuBar )
  268.     {
  269.         DrawMenuBar( );
  270.         *pRedrawMenuBar = false;
  271.     }
  272. }
  273.  
  274.  
  275. #pragma segment Main
  276.  
  277. void    SetUpCursors( void )
  278. {
  279.     CursHandle  hCurs;
  280.  
  281.     hCurs = GetCursor( iBeamCursor );
  282.     editCursor = **hCurs;
  283.     hCurs = GetCursor( watchCursor );
  284.     waitCursor = **hCurs;
  285. }
  286.  
  287. #pragma segment Main
  288.  
  289. void    SetUpMenus( void )
  290. {
  291.     short i;
  292.     
  293.     myMenus[appleM] = GetMenu( appleID );
  294.     AddResMenu( myMenus[appleM], 'DRVR' );
  295.     myMenus[fileM] = GetMenu( fileID );
  296.     myMenus[editM] = GetMenu( editID );
  297.     myMenus[fontM] = GetMenu( mfontID );
  298.     AddResMenu( myMenus[fontM], 'FONT' );
  299.     myMenus[sizeM]  = GetMenu( sizeID );
  300.     myMenus[styleM] = GetMenu( styleID );
  301.     myMenus[scriptM] = GetMenu( mscriptID );
  302.     myMenus[subroutineM] = GetMenu( subroutineID );
  303.  
  304.     for (i = appleM; i <= kLastMenu; i++)
  305.         InsertMenu( myMenus[i], 0 );
  306.  
  307.     SetItemStyle( myMenus[styleM], cPlain, 0 );
  308.     SetItemStyle( myMenus[styleM], cBold, bold );
  309.     SetItemStyle( myMenus[styleM], cItalic, italic );
  310.     SetItemStyle( myMenus[styleM], cUnderline, underline );
  311.     SetItemStyle( myMenus[styleM], cOutline, outline );
  312.     SetItemStyle( myMenus[styleM], cShadow, shadow );
  313.     SetItemStyle( myMenus[styleM], cCondense, condense );
  314.     SetItemStyle( myMenus[styleM], cExtend, extend );
  315.  
  316.     SetShortMenus( ); /* Does a DrawMenuBar() */
  317. }
  318.  
  319. void    CheckMenus( void )
  320. {
  321.     static long    lastCount = -1;    // -1 for first time through
  322.     long        nowCount = CountDocuments( );
  323.  
  324.     if ( lastCount < 0
  325.             || ( ! lastCount && nowCount )
  326.                 || ( lastCount && ! nowCount ) )
  327.     {
  328.         if ( CountDocuments( ) > 0 )
  329.             SetLongMenus( );
  330.         else
  331.             SetShortMenus( );
  332.     }
  333.     
  334.     lastCount = nowCount;
  335. }
  336.  
  337.  
  338. //    Use process manager to find the volume and directory of this application.
  339.  
  340. void    InitApplicationRec( void )
  341. {
  342.     ProcessSerialNumber        PSN;
  343.     ProcessInfoRec            info;
  344.     OSErr                    err;
  345.     
  346.     gAppRec.theScriptID = kOSANullScript;
  347.  
  348.     err = GetCurrentProcess( &PSN );
  349.  
  350.     if ( noErr == err )
  351.     {
  352.         info.processName = gAppRec.theName;
  353.         info.processAppSpec = &gAppRec.theSpec;
  354.  
  355.         err = GetProcessInformation( &PSN, &info );
  356.     }
  357. }
  358.  
  359.  
  360. void    DoFile( short theItem )
  361. {        
  362.     short   alertResult;
  363.     DPtr    theDoc = nil;
  364.     FSSpec  theFSSpec;
  365.     OSErr   fileErr;
  366.     TPrint  thePSetup;
  367.     
  368.     theItem = ConvertMenuActualToGXMenu ( theItem );
  369.  
  370.     switch (theItem)
  371.     {
  372.         case fmNew :
  373.             IssueAENewWindow( );
  374.             break;
  375.  
  376.         case fmOpen:
  377.             if ( GetFile( &theFSSpec ) == noErr )
  378.                 fileErr = IssueAEOpenDoc( theFSSpec );
  379.             break;
  380.  
  381.         case fmClose:
  382.             IssueCloseCommand( FrontWindow( ) );
  383.             break;
  384.  
  385.         case fmSave:
  386.         case fmSaveAs:
  387.             theDoc = DPtrFromWindowPtr( FrontWindow( ) );
  388.                 
  389.             if (theDoc->everSaved == false || theItem == fmSaveAs)
  390.             {
  391.                 fileErr = GetFileNameToSaveAs(theDoc);
  392.                 if (fileErr == userCanceledErr)
  393.                     break;
  394.                 else if (fileErr != noErr)
  395.                     FileError((unsigned char *)"\pError saving ", theDoc->theFileName);
  396.                 else
  397.                     fileErr = IssueSaveCommand(theDoc->theWindow, &theDoc->theFSSpec);
  398.             
  399.                 if (fileErr == noErr)
  400.                 {
  401.                     SetWTitle(theDoc->theWindow, theDoc->theFSSpec.name);
  402.                     theDoc->everSaved = true;
  403.                 }
  404.             }
  405.             else
  406.                 fileErr = IssueSaveCommand(theDoc->theWindow, nil);
  407.             break;
  408.  
  409.         case fmRevert:
  410.             alertResult = DoFileDialog ( kRevertDialog, FrontWindow ( ) );
  411.             if ( alertResult == kStdOkItemIndex )
  412.             {
  413.                 if (IssueRevertCommand(theDoc->theWindow))
  414.                     FileError((unsigned char *)"\pError reverting ", theDoc->theFileName);
  415.             }
  416.             break;
  417.  
  418.         case fmPageSetUp:
  419.             theDoc = DPtrFromWindowPtr(FrontWindow());
  420.             if (gGXIsPresent)
  421.             {
  422.                 if (DoGXPageSetup(theDoc))
  423.                     IssueGXPageSetupWindow(theDoc->theWindow, theDoc->documentJob);
  424.             }
  425.             else if (DoPageSetup(theDoc))
  426.             {
  427.                 thePSetup = **(theDoc->thePrintSetup);
  428.                  IssuePageSetupWindow(theDoc->theWindow, thePSetup);
  429.              }
  430.             break;
  431.  
  432.         case fmPrint:
  433.             IssuePrintWindow(FrontWindow(),kUsePrintDialog);
  434.             break;
  435.             
  436.         case fmPrintOne:
  437.             IssuePrintWindow(FrontWindow(),kNoPrintDialog);
  438.             break;
  439.  
  440.         case fmQuit:
  441.             IssueQuitCommand();
  442.             break;
  443.     } // End of switch
  444. }
  445.  
  446.  
  447. #pragma segment Main
  448.  
  449. void    DoCommand( long mResult )
  450. {
  451.     DoMenuItem( HiWord( mResult ), LoWord( mResult ) );
  452. }
  453.  
  454. void    DoMenuItem( short theMenuID, short theItem )
  455. {
  456.     long        result;
  457.     Str255      name;
  458.     DPtr        theDocument;
  459.     Boolean     exists;
  460.     Boolean     useOldCode;
  461.     Boolean        wasDirty;
  462.     short       err;
  463.  
  464.     theDocument = DPtrFromWindowPtr( GetNthDocument( 1 ) );
  465.  
  466.     err = ScriptForMenuExists( theMenuID, theItem, &exists );
  467.  
  468.     if ( err == noErr && exists == true )
  469.     {
  470.         err = ExecuteScriptForMenu( theMenuID, theItem );
  471.         useOldCode = ( noErr != err && errOSAScriptError != err );
  472.     }
  473.     else
  474.         useOldCode = true;
  475.         
  476.     if ( ! useOldCode )        // Script did whatever without error or no script
  477.         return;
  478.  
  479.     switch ( theMenuID )
  480.     {
  481.         case appleID:
  482.             if (theItem == aboutItem)
  483.             {
  484.                 SetCursor(&qd.arrow);
  485.                 result = Alert( AboutAlert, NULL );
  486.             }
  487.             else
  488.             {
  489.                 GetItem(myMenus[appleM], theItem, name);
  490.                 err = OpenDeskAcc(name);
  491.                 SetPort(FrontWindow());
  492.             }
  493.             break;
  494.             
  495.         case fileID:
  496.             DoFile(theItem);
  497.             break;
  498.  
  499.         case editID:
  500.             if (SystemEdit(theItem - 1) == false)
  501.             {
  502.                 switch (theItem)
  503.                 {
  504.                     case cutCommand:
  505.                         IssueCutCommand(theDocument);
  506.                         break;
  507.                                                  
  508.                     case copyCommand:
  509.                         IssueCopyCommand(theDocument);
  510.                         break;
  511.                                                  
  512.                     case pasteCommand:
  513.                         IssuePasteCommand(theDocument);
  514.                         break;
  515.                                                  
  516.                     case clearCommand:
  517.                         IssueClearCommand(theDocument);
  518.                         break;
  519.                                                  
  520.                     case selectAllCommand:
  521.                         if (theDocument)
  522.                             TESetSelect(0, (**(theDocument->theText)).teLength, theDocument->theText);
  523.                        break;
  524.                 }
  525.                 
  526.                 ShowSelect(theDocument);
  527.             }
  528.             break;
  529.  
  530.         case mfontID:
  531.             IssueFontCommand(theDocument, theItem);
  532.             break;
  533.  
  534.         case sizeID:
  535.             IssueSizeCommand(theDocument, theItem);
  536.             break;
  537.  
  538.         case styleID:
  539.             IssueStyleCommand(theDocument, theItem);
  540.             break;
  541.  
  542.         case mscriptID:
  543.             switch (theItem)
  544.             {
  545.                 case cCompile:
  546.                     CompileDocument(theDocument);
  547.                     break;
  548.  
  549.                 case cExecute:
  550.                     ExecuteDocument(theDocument);
  551.                     break;
  552.  
  553.                 case cResultWindow:
  554.                     if ( IsThereAResultWind( ) )
  555.                         IssueCloseCommand( GetResultsWindPtr( ) );
  556.                     else
  557.                         OpenResultWind( );
  558.                     break;
  559.             }
  560.             break;
  561.             
  562.         case subroutineID:
  563.             switch ( theItem )
  564.             {
  565.                 case cScript1:
  566.                     ExecuteScript1(theDocument);
  567.                     break;
  568.  
  569.                 case cScript2:
  570.                     ExecuteScript2(theDocument);
  571.                     break;
  572.                 
  573.                 case cScript3:
  574.                     ExecuteScript3(theDocument);
  575.                     break;
  576.                 
  577.                 case cScript4:
  578.                     wasDirty = theDocument->dirty;
  579.                     theDocument->dirty = true;
  580.                     DoFile(fmSaveAs);
  581.                     
  582.                     if (! theDocument->dirty)
  583.                     ExecuteScript4(theDocument);
  584.  
  585.                     else
  586.                     theDocument->dirty = wasDirty;
  587.                     break;
  588.             }
  589.             break;
  590.     }
  591.  
  592.     HiliteMenu(0);
  593. }
  594.  
  595. #pragma segment Main
  596.  
  597.  
  598. void    DoMouseDown( const EventRecord *myEvent )
  599. {
  600.     WindowPtr    whichWindow;
  601.     Point        p;
  602.     Rect        dragRect,
  603.                 oldRect = { 0, 0, 0, 0 };
  604.     DPtr        theDoc;
  605.     long        menuResult;
  606.     Boolean        scriptExists;
  607.     OSErr        err;
  608.  
  609.     p = myEvent->where;
  610.     switch ( FindWindow( p, &whichWindow ) )
  611.     {
  612.         case inDesk:
  613.             SysBeep(10);
  614.             break;
  615.  
  616.         case inGoAway:
  617.             if ( Ours( whichWindow ) )
  618.                 if ( TrackGoAway( whichWindow, p ) )
  619.                     IssueCloseCommand(whichWindow);
  620.             break;
  621.  
  622.         case inMenuBar:
  623.             SetCursor( &qd.arrow );
  624.             theDoc = DPtrFromWindowPtr( FrontWindow( ) );
  625.             if ( theDoc )
  626.                 SetFontMenu( theDoc );
  627.             
  628.             menuResult = MenuSelect( p );
  629.  
  630.                 // Check for script editing -
  631.                 // ctrl+option when selecting the menu = Edit Script (if any)
  632.             
  633.             if ( OptionKeyPressed( myEvent )  && CtrlKeyPressed( myEvent ) )
  634.             {
  635.                 err = ScriptForMenuExists( HiWord( menuResult ),
  636.                                         LoWord( menuResult ), &scriptExists );
  637.  
  638.                 if ( scriptExists && noErr == err )
  639.                     EditMenuScript( HiWord( menuResult ), LoWord( menuResult ) );
  640.                 else
  641.                     SysBeep( 10 );
  642.             }
  643.             else
  644.                 DoCommand( menuResult );
  645.  
  646.             HiliteMenu( 0 );
  647.             break;
  648.  
  649.         case inSysWindow:
  650.             SystemClick(myEvent, whichWindow);
  651.             break;
  652.  
  653.         case inDrag:
  654.             dragRect = qd.screenBits.bounds;
  655.             
  656.             if ( Ours( whichWindow ) )
  657.             {
  658.                 (void)GetWindowBounds( whichWindow, &oldRect );
  659.             
  660.                 DragWindow( whichWindow, p, &dragRect );
  661.  
  662.                 //    As rgnBBox may be passed by address
  663.  
  664.                 (void)GetWindowBounds( whichWindow, &dragRect );
  665.  
  666.                 //    The windows already there, but still tell
  667.                 //    the our AppleEvents core about the move in case
  668.                 //    they want to do anything
  669.                 
  670.                 if ( ! EqualRect( &oldRect, &dragRect ) )
  671.                  IssueMoveWindow( whichWindow, &dragRect );
  672.             }
  673.             break;
  674.  
  675.         case inGrow:
  676.             SetCursor(&qd.arrow);
  677.             if (Ours(whichWindow))
  678.                 MyGrowWindow(whichWindow, p);
  679.             break;
  680.                             
  681.         case inZoomIn:
  682.             DoZoom(whichWindow, inZoomIn, p);
  683.             break;
  684.  
  685.         case inZoomOut:
  686.             DoZoom(whichWindow, inZoomOut, p);
  687.             break;
  688.             
  689.         case inContent:
  690.             if (whichWindow != FrontWindow())
  691.                 SelectWindow(whichWindow);
  692.             else if (Ours(whichWindow))
  693.                 DoContent(whichWindow, *myEvent);
  694.             break;
  695.     } // End of switch
  696. }
  697.  
  698. #pragma segment Main
  699.  
  700. long    GetSleep( void )
  701. {
  702.     long      sleep;
  703.     WindowPtr theWindow;
  704.     DPtr      theDoc;
  705.  
  706.     sleep = LONG_MAX;
  707.     if ( ! gInBackground )
  708.     {
  709.         theWindow = FrontWindow();
  710.         if (theWindow)
  711.             if (Ours(theWindow))
  712.             {
  713.                 theDoc = DPtrFromWindowPtr(theWindow);
  714.                 if ((**(theDoc->theText)).selStart == (**(theDoc->theText)).selEnd)
  715.                     sleep = GetCaretTime();
  716.             }
  717.     }
  718.  
  719.     return sleep;
  720. } // GetSleep
  721.  
  722. #pragma segment Main
  723.  
  724. void    HandleOneEvent( EventRecord *myEvent )
  725. {
  726.     DPtr        theDoc;
  727.     char        theChar;
  728.     WindowPtr   theWindow;
  729.     Boolean     activate;
  730.     
  731.     switch (myEvent->what)
  732.     {
  733.         case mouseDown:
  734.             FlushAndRecordTypingBuffer();
  735.             DoMouseDown(myEvent);
  736.             break;
  737.             
  738.         case keyDown:
  739.         case autoKey:
  740.             theDoc = DPtrFromWindowPtr(FrontWindow());
  741.             
  742.             theChar = myEvent->message & charCodeMask;
  743.             
  744.             if ((myEvent->modifiers & cmdKey) == cmdKey)
  745.             {
  746.                 DoCommand(MenuKey(theChar));
  747.                 HiliteMenu(0);
  748.             }
  749.             else if ( ( theDoc->theText ) && ( theDoc->windowType == kOrdinaryWind ) )
  750.             {
  751.                 AddKeyToTypingBuffer(theDoc, theChar);
  752.                 
  753.                 TEKey(theChar, theDoc->theText);
  754.                 AdjustScrollbars(theDoc, false);
  755.         
  756.                 ShowSelect(theDoc);
  757.                 
  758.                 theDoc->dirty = true;
  759.             }
  760.             break;
  761.             
  762.         case activateEvt:
  763.             activate = ((myEvent->modifiers & activeFlag) != 0);
  764.             theWindow = (WindowPtr)myEvent->message;
  765.             DoActivate(theWindow, activate);
  766.             break;
  767.  
  768.         case updateEvt:
  769.             DoUpdate ( (WindowPtr) myEvent->message );
  770.             break;
  771.  
  772.         case kHighLevelEvent:
  773.             FlushAndRecordTypingBuffer();
  774.             DoAppleEvent(*myEvent);
  775.             break;
  776.  
  777.         case osEvt:
  778.             if ( ( myEvent->message >> 24 ) & suspendResumeMessage )    // suspend or resume
  779.             {
  780.                 gInBackground = ( ( myEvent->message & resumeFlag ) == 0 );
  781.                 DoActivate( FrontWindow( ), ! gInBackground );
  782.             }
  783.             break;
  784.     }
  785. }
  786.     
  787. #pragma segment Main
  788.  
  789. void    MainEvent( void )
  790. {
  791.     EventRecord myEvent;
  792.     Boolean     redrawMenuBar = false;
  793.     
  794.     MaintainCursor(); // TEIdle in here for now
  795.     MaintainMenus( &redrawMenuBar );
  796.     
  797.     if (WaitNextEvent(everyEvent, &myEvent, GetSleep(), nil))
  798.         HandleOneEvent( &myEvent );
  799. }
  800.  
  801. #pragma segment Main
  802.  
  803. OSErr GXPrintingEventOverride(EventRecord *anEvent, Boolean filterEvent)
  804. {
  805.     // Handle events in whatever way is appropriate. MyDoEvent is
  806.     //    our generic event handler. We don't pass it events that it
  807.     //    shouldn't handle while printing dialogs are displayed.
  808.  
  809.     if (!filterEvent)
  810.         switch (anEvent->what) {
  811.             case mouseDown:
  812.             case keyDown:
  813.             case autoKey:
  814.                 break;
  815.             default:
  816.                 HandleOneEvent( anEvent );
  817.         }
  818.     return noErr;
  819. }
  820.  
  821. #pragma segment Main
  822.  
  823. void    DoMenuScripter( void )
  824. {
  825.     OSErr  err;
  826.     short  result;
  827.  
  828.     InitGraf( &qd.thePort );
  829.     InitFonts( );
  830.     FlushEvents( everyEvent, 0 );
  831.     InitWindows( );
  832.     InitMenus( );
  833.     TEInit( );
  834.     InitDialogs( NULL );
  835.     InitCursor( );
  836.  
  837.     MaxApplZone( );
  838.     SetUpCursors( );
  839.     
  840.     SetCursor( &waitCursor );
  841.  
  842.     err = TEFromScrap();    // Get the Scrap
  843.  
  844.     SetUpMenus( );
  845.  
  846.     gNewDocCount    = 0;
  847.     gFontMItem        = 0;
  848.  
  849.     gQuitting                    = false;
  850.  
  851.     gGestaltAvailable            = false;
  852.     gAppleEventsImplemented        = false;
  853.     gAliasManagerImplemented    = false;
  854.     gEditionManagerImplemented    = false;
  855.     gOutlineFontsImplemented    = false;
  856.     gGXIsPresent                = false;
  857.  
  858.         // Check environment checks to see if we are running 7.0
  859.     if ( ! CheckEnvironment( ) )
  860.     {
  861.         SetCursor( &qd.arrow );
  862.             // Pose the only 7.0 alert
  863.         result = Alert( NeedSys7Alert, NULL );
  864.         return;
  865.     }
  866.     
  867.         // Do before InitEditorScripting() and SetUpScripts()
  868.     InitApplicationRec( );
  869.  
  870.         // Init GX Printing
  871.     InitGXIfPresent( );
  872.  
  873.     err = AEObjectInit( );
  874.     if ( noErr != err )
  875.     {
  876.         ShowError((unsigned char *)"\pAEObjectInit", err);
  877.         goto done;
  878.     }
  879.  
  880.     InitAppleEvents( );
  881.  
  882.     err = PPCInit( );
  883.     if ( noErr != err )
  884.     {
  885.         ShowError((unsigned char *)"\pPPCInit", err);
  886.         goto done;
  887.     }
  888.         
  889.     err = InitDragHandlers( );
  890.     if ( noErr != err )
  891.     {
  892.         ShowError((unsigned char *)"\pInitDragHandlers", err);
  893.         goto done;
  894.     }
  895.     
  896.     err = InitEditorScripting( );
  897.     if ( noErr != err )
  898.     {
  899.         ShowError((unsigned char *)"\pInitEditorScripting", err);
  900.         goto done;
  901.     }
  902.     
  903.     SetUpScripts( );
  904.                               
  905.     SetOSAActiveProcedure( );    // Set after all of our scripts are loaded
  906.         
  907.     // Create the UPPs for the control tracking routines
  908.     //  globals, since we don't want to have to create and dispose of the routine
  909.     //  descriptors each time we call DoContent
  910.     gHScrollActionUPP = NewControlActionProc ( HActionProc );
  911.     gVScrollActionUPP = NewControlActionProc ( VActionProc );
  912.     
  913.     // Create a UPP for drawing a dialog's default button. Again, make it a global
  914.     //  for convenience
  915.     gDefaultButtonUPP = NewUserItemProc( DrawDefaultOutline );
  916.  
  917.     SetCursor( &qd.arrow );
  918.  
  919.     while ( ! gQuitting )
  920.         MainEvent( );
  921.  
  922.     SetCursor( &waitCursor );
  923.  
  924. done:
  925.     (void)CleanUpAEScripts( );
  926.     (void)CloseEditorScripting( );
  927.     CleanUpGXIfPresent( );
  928. }
  929.     
  930. void    main( )
  931. {
  932.     // The main routine starts here
  933.     DoMenuScripter();
  934. }